package com.health.httpbean.format;

import com.blankj.utilcode.util.TimeUtils;
import com.health.db.FDbManager;
import com.health.httpbean.model.JsbBean;
import com.health.httpbean.model.MedicineBean;
import com.health.tablebean.BD_DD_INFO;
import com.health.tablebean.CD_DRUGCONDITION;
import com.health.tablebean.CD_MENTALDISEASE_FOLLOWUP;
import com.health.viewscontrols.NovaApplication;

import net.tsz.afinal.db.sqlite.DbModel;

import java.util.ArrayList;
import java.util.List;

import static com.health.httpbean.format.FormatUtils.formatAskSymptom;
import static com.health.httpbean.format.FormatUtils.parseInt;
import static com.health.httpbean.format.FormatUtils.parseString;

/**
 * author : xiaoxin
 * date   : 2020/12/17 16:43
 * desc   : 精神障碍患者
 * update :
 */
public class FormatJsbData {

    public static JsbBean formatJsb(CD_MENTALDISEASE_FOLLOWUP table, List<BD_DD_INFO> listZZs) {
        JsbBean bean = new JsbBean();

        try {
            bean.CardID = table.getIDCARD();
//        bean.ID_UserDocument = table.getARCHIVEID();
            String userSql = "SELECT * FROM ARCHIVE_BASEINFO WHERE IDCARD = '" + table.getIDCARD() + "'";
            DbModel userModel = NovaApplication.fdbmanger.findDbSql(userSql);
            bean.UserName = userModel.getString("CUSTOMERNAME");
            bean.BianHao = table.getCUSTOMERID();
            try {
                bean.SuiFangRiQi = TimeUtils.millis2String(
                        table.getFOLLOWUP_DATE().getTime(), "yyyy-MM-dd");
            } catch (Exception e) {
                e.printStackTrace();
            }
            //有无转诊   DM01-11_2 有 DM01-11_1 无
            bean.ZhuanZhenQingKuang = table.getWHETHER_REFERRAL() == "DM01-11_2" ? 2 : 1;
            bean.ZhuanZhenYuanYin = table.getREFERRAL_REASON();
            bean.ZhuanZhenJiGou = table.getREFERRAL_AGENC_DEPAR();
            try {
                bean.XiaCiSuiFangRiQi = TimeUtils.millis2String(
                        table.getNEXTFOLLOWUP_DATE().getTime(), "yyyy-MM-dd");
            } catch (Exception e) {
                e.printStackTrace();
            }

//        bean.SuiFangYiSheng = table.getFOLLOWUPDOCTOR();
            String sql = "SELECT * FROM BD_ORGANIZE_EMPLOYEE WHERE USER_ID = '" + table.getFOLLOWUPDOCTOR() + "'";
            DbModel dbSql = NovaApplication.fdbmanger.findDbSql(sql);
            bean.SuiFangDoctorName = dbSql.getString("NAME");

            //随访类型、随访分类 :1门诊 2家庭 3电话 哪个是  是不是这里接口多了一个字段
            //DM04-15_1 解析出错
            bean.SuiFangLeiXing = parseInt(table.getFOLLOWUP_CLASSIFICAT());
            bean.SuiFangFangShi = parseInt(table.getFOLLOWUP_TYPE());

            bean.ShiFangYuanYin = parseInt(table.getLossFollow());
            try {
                bean.SiWangShiJian = TimeUtils.millis2String(
                        table.getDeathDate().getTime(), "yyyy-MM-dd");
            } catch (Exception e) {
                e.printStackTrace();
            }
//        bean.JSBID = table.getID();
            //死亡原因 int 改成string
            bean.JSBSiWangYuanYin = parseInt(table.getDeathCause());
            bean.JSBQuTiJiBing = parseInt(table.getDeathCauseBody());
            bean.JSBWeiXianXingPingGu = parseInt(table.getFATALNESS());
            bean.JSBMuQianZhengZhuang = formatAskSymptom(table.getPRESENT_SYMPTOM(),listZZs);
            bean.JSBMuQianZhengZhuangQiTa = parseString(table.getPRESENT_SYMPTO_OTHER());
            bean.JSBZiZhiLi = parseInt(table.getINSIGHT());
            bean.JSBShuiMianQingKuang = parseInt(table.getSLEEP_QUALITY());
            bean.JSBYinShiQingKuang = parseInt(table.getDIET());
            bean.JSBGeRenShengHuoLiaoLi = parseInt(table.getPERSONAL_CARE());
            bean.JSBJianWuLaoDong = parseInt(table.getHOUSEWORK());
            bean.JSBShengChanLaoDong = parseInt(table.getPRODUCT_LABOR_WORK());
            bean.JSBXueXiNengLi = parseInt(table.getLEARNING_ABILITY());
            bean.JSBSheHuiRenJiJiaoWang = parseInt(table.getSOCIAL_INTER_INTERA());
            bean.JSBZiShi = parseString(table.getMILD_TROUBLE_FREQUEN());
            bean.JSBZhaoShi = parseString(table.getCREATE_DISTU_FREQUEN());
            bean.JSBZhaoHuo = parseString(table.getCAUSE_ACCID_FREQUEN());
            bean.JSBQiTaWeiXian = parseString(table.getFATALNESS());
            bean.JSBZiShang = parseString(table.getAUTOLESION_FREQUEN());
            bean.JSBZiShaWeiSui = parseString(table.getATTEMPT_SUIC_FREQUEN());
            bean.JSBQiJianGuanSuoQingKuang = parseInt(table.getLOCK_CONDITION());
            bean.JSBQiJianZhuYuanQingKuang = parseInt(table.getHOSPITALIZATI_STATUS());
            bean.JSBMoCiChuYuanShiJian =  TimeUtils.millis2String(
                    table.getLAST_LEAVE_HOSP_TIME().getTime(), "yyyy-MM-dd");
            bean.JSBShiYanShiJianCha = parseInt(table.getLABOR_EXAMINATI());
            bean.JSBShiYanShiJianChaMiaoShu = parseString(table.getLABOR_EXAMINATI_HAVE());
            bean.JSBYongYaoYiCongXing = parseInt(table.getMEDICATIO_COMPLIANCE());
            bean.JSBYaoWuBuLiangFanYingQingKuang = parseInt(table.getADVER_DRU_REACT());
            bean.JSBYaoWuBuLiangFanYingMiaoShu = parseString(table.getADVER_DRU_REACT_HAVE());
            bean.JSBZhiLiaoXiaoGuo = parseInt(table.getTREATMENT_EFFECT());
            bean.JSBKangFuCuoShi = parseString(table.getREHABILI_MEASU());
            bean.JSBKangFuCuoShiQiTa = parseString(table.getREHABILI_MEASU_OTHER());
            bean.JSBSuiFangFenLei = parseInt(table.getFOLLOWUP_CLASSIFICAT());

            String strWhere = "IDCARD = '" + table.getIDCARD() + "'";
            List<CD_DRUGCONDITION> drugList = NovaApplication.fdbmanger.findAllByWhere(CD_DRUGCONDITION.class,
                    strWhere + "AND TYPE = '3'");
            List<BD_DD_INFO> unitList = FDbManager.getInstance(NovaApplication.getInstance())
                    .findAllByWhereInInfoDB(BD_DD_INFO.class,
                            "PARENTID='DM04-05'" + " order by cast(SORT as int) asc");
            List<MedicineBean> listMedicine = new ArrayList<>();
            for (int i = 0; i < drugList.size(); i++) {
                MedicineBean medicineBean = new MedicineBean();
                medicineBean.MedicineName = drugList.get(i).getNAME();
                medicineBean.Dosage = drugList.get(i).getEVERYTIMEMG();
                for (int j = 0; j < unitList.size(); j++) {
                    if (drugList.get(i).getRES1() != null
                            && drugList.get(i).getRES1().equals(unitList.get(j).getID())) {
                        medicineBean.Unit = unitList.get(j).getDATAINFO();
                    }
                }

                String dailytime = drugList.get(i).getDAILYTIME();
                if(null != dailytime && !dailytime.equals("") && dailytime.contains("/")){
                    String[] split = dailytime.split("/");
                    if(null != split && !split.equals("")){
                        String s = split[1];
                        if(null != s && !s.equals("")){
                            if(s.equals("99")){
                                medicineBean.Method = FormatUtils.drugMethod(23);
                            }else{
                                medicineBean.Method = FormatUtils.drugMethod(Integer.parseInt(s)-1);
                            }
                        }
                        String s1 = split[0];
                        if(null != s1 && !s1.equals("")){
                            if(s1.equals("99")){
                                medicineBean.Cycle = FormatUtils.cycle(14);
                            }else{
                                medicineBean.Cycle = FormatUtils.cycle(Integer.parseInt(s1)-1);
                            }
                        }
                    }

                }
                listMedicine.add(medicineBean);
            }
            bean.listMedicine = listMedicine;

            String strWhere1 = "IDCARD = '" + table.getIDCARD() + "'";
            List<CD_DRUGCONDITION> drugList1 = NovaApplication.fdbmanger.findAllByWhere(CD_DRUGCONDITION.class,
                    strWhere1 + "AND TYPE = '6'");
            for (int i = 0; i < drugList1.size(); i++) {

            }

            List<MedicineBean> listZhiDaoMedicine = new ArrayList<>();
            for (int i = 0; i < drugList1.size(); i++) {
                MedicineBean medicineBean = new MedicineBean();
                medicineBean.MedicineName = drugList1.get(i).getNAME();
                medicineBean.Dosage = drugList1.get(i).getEVERYTIMEMG();
                for (int j = 0; j < unitList.size(); j++) {
                    if (drugList1.get(i).getRES1() != null
                            && drugList1.get(i).getRES1().equals(unitList.get(j).getID())) {
                        medicineBean.Unit = unitList.get(j).getDATAINFO();
                    }
                }

                String dailytime = drugList1.get(i).getDAILYTIME();
                if(null != dailytime && !dailytime.equals("") && dailytime.contains("/")){
                    String[] split = dailytime.split("/");
                    if(null != split && !split.equals("")){
                        String s = split[1];
                        if(null != s && !s.equals("")){
                            if(s.equals("99")){
                                medicineBean.Method = FormatUtils.drugMethod(23);
                            }else{
                                medicineBean.Method = FormatUtils.drugMethod(Integer.parseInt(s)-1);
                            }
                        }
                        String s1 = split[0];
                        if(null != s1 && !s1.equals("")){
                            if(s1.equals("99")){
                                medicineBean.Cycle = FormatUtils.cycle(14);
                            }else{
                                medicineBean.Cycle = FormatUtils.cycle(Integer.parseInt(s1)-1);
                            }
                        }
                    }

                }
                listZhiDaoMedicine.add(medicineBean);
            }
            bean.listZhiDaoMedicine = listZhiDaoMedicine;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        return bean;
    }
}
